package com.cloudwise.dao.impl;

import com.cloudwise.dao.EmpDao;
import com.cloudwise.entity.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2021/11/17 11:12
 * @description：通过springjdbc实现的dao实现类
 * @modified By：
 * @version: 1.0
 */
@Repository
public class EmpDaoSpringJdbcImpl  implements EmpDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public List<Map<String, Object>> findAllEmp() {
        String sql="select * from emp ";
        List<Map<String, Object>> mapList = jdbcTemplate.queryForList(sql);
        return mapList;
    }
    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description:查询所有的emp，返回实体类类型
     *
     * 	 * @param sql the SQL query to execute
     * 	 * @param rowMapper a callback that will map one object per row
     *
     * @create time: 2021/11/17 14:46
     * @return java.util.List<com.cloudwise.entity.Emp>
     */
    @Override
    public List<Emp> findAllEmpEntity() {
        String sql="select * from emp ";
       /* List<Emp> empList = jdbcTemplate.query(sql, new RowMapper<Emp>() {
            public Emp mapRow(ResultSet rs, int rowNum) throws SQLException {
                Emp emp = new Emp();
                emp.setEmpno(rs.getInt("empno"));
                emp.setEname(rs.getString("ename"));
                emp.setJob(rs.getString("job"));
                emp.setMgr(rs.getInt("mgr"));
                emp.setSal(rs.getDouble("sal"));
                emp.setComm(rs.getDouble("comm"));
                emp.setHiredate(rs.getDate("hiredate"));
                emp.setDeptno(rs.getInt("deptno"));
                return emp;
            }
        });*/
        List<Emp> empList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Emp>(Emp.class));
        return empList;
    }

    @Override
    public boolean addEmp(Map map) {
        String sql="insert into emp (empno,ename,deptno) values (?,?,?)";
        int update = jdbcTemplate.update(sql, map.get("empno"), map.get("ename"), map.get("deptno"));
        if(update>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteEmpById(Integer empno) {
        String sql="delete  from emp where empno=?";
        int update = jdbcTemplate.update(sql, empno);
        if(update>0){
            return true;
        }
        return false;
    }
}
